/*
 * Copyright 2016-2021 Pnoker. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ac.driver.mqtt.service.impl;

import com.ac.driver.mqtt.util.MqttUtils;
import com.xj.common.bean.constant.Common;
import com.xj.common.bean.em.ControlStateEm;
import com.xj.common.model.ControlInfo;
import com.xj.common.sdk.bean.driver.DriverContext;
import com.xj.common.sdk.service.ControlService;
import com.xj.common.sdk.service.DriverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @author pnoker
 */
@Slf4j
@Service
public class ControlServiceImpl implements ControlService {

    @Resource
    private DriverService driverService;
    @Resource
    private DriverContext driverContext;

    @Override
    public void sendCtl(ControlInfo controlInfo) {
        Long deviceId = driverContext.getDriverMetadata().getDeviceSnMap().get(controlInfo.getDeviceSn()).getId();
        controlInfo.setDeviceId(deviceId);
        controlInfo.setReqtime(new Date());//记录请求时间
        controlInfo.setMsgType(Common.Device.ControlEvent.REQUEST); //标记为，代表上送到物联网平台的是一个请求类
        driverService.controlInfoSender(controlInfo);  //发送到物联网平台，有ie-data中的ControlInfoConsumer接收
        if(controlInfo.getTopic() == null||controlInfo.getTopic().trim().equals(""))
            controlInfo.setTopic("/CHARGE/"+ controlInfo.getDeviceSn()+"/M2S/request");
        //组装dataBody待完善
        String dataBody="";
        //下发到装置
        MqttUtils.sendMessage(controlInfo.getTopic(),dataBody,"topic-device");
    }

    /**
     * 下发充电命令
     * @param dataBody   下发到装置的消息体
     * @param topic  下发装置的topic,若为空则组装默认的
     * @param sn   目标设备的SN
     * @param clientid  下发控令的客户端id,上送到物联网平台的有关用户信息
     * @return
     */
    @Override
    public Long snedCtl(String dataBody, String topic,String sn,String clientid) {
        if(topic == null||topic.trim().equals(""))
            topic = "/CHARGE/"+ sn+"/M2S/request";
        //控制数据上报给Data服务
        Long deviceId = driverContext.getDriverMetadata().getDeviceSnMap().get(sn).getId();
        ControlInfo controlInfo = new ControlInfo(deviceId,clientid, ControlStateEm.CTL_STATE_RECEIVED.getCode(), dataBody);
        controlInfo.setReqtime(new Date());//记录请求时间
        controlInfo.setMsgType(Common.Device.ControlEvent.REQUEST); //标记为，代表上送到物联网平台的是一个请求类
        Long id = System.currentTimeMillis();//生成一个id
        controlInfo.setId(id.toString());
        driverService.controlInfoSender(controlInfo);  //发送到物联网平台，有ie-data中的ControlInfoConsumer接收
        //下发到装置
        MqttUtils.sendMessage(topic,dataBody,"topic-device");

        return id;
    }

    @Override
    public Boolean snedState(String dataBody, String topic,String deviceid) {
        if(topic == null||topic.trim().equals(""))
            topic ="/CHARGE/"+ deviceid+"/S2M/state";

        MqttUtils.sendMessage(topic,dataBody,"topic-device");
        //调用manager的SPI接口，写入redis本次测试的数据，在handler中接收到response数据的时候，更新空置状态
        return true;
    }
}
